package com.itheima.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author AxeLai
 * @date 2019-05-14 15:09
 */
public class MapUtils {
    final static String AK = "Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi";

//http://api.map.baidu.com/geocoding/v3/?address=%E5%8C%97%E4%BA%AC%E5%B8%82%E6%B5%B7%E6%B7%80%E5%8C%BA%E4%B8%8A%E5%9C%B0%E5%8D%81%E8%A1%9710%E5%8F%B7&output=json&ak=Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi
    /**
     * 地理编码 URL
     */
    final static String MAP_URL = "http://api.map.baidu.com/geocoding/v3/?output=json&ak="+AK;
    //http://api.map.baidu.com/geocoding/v3/?ak=Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194
    //http://api.map.baidu.com/reverse_geocoding/v3/?ak=Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi&&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194
   // http://api.map.baidu.com/reverse_geocoding/v3/?ak=Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi&output=json&coordtype=wgs84ll&location=31.225696563611,121.49884033194
    final static String MAP_URL1 ="http://api.map.baidu.com/reverse_geocoding/v3/?ak=Du0e8vPZ92UuwMi8N6Ldj759y5WOYKfi&output=json&coordtype=wgs84ll";
    public static String getAddress(double lng, double lat) {
        String address = "";
        String location = lat + "," + lng;
        BufferedReader in = null;
        URL url = null;
        URLConnection connection = null;
        try {
            url = new URL(MAP_URL1+"&location="+lng+","+lat);
            connection = url.openConnection();
            connection.setDoOutput(true);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String line;
            StringBuilder text = new StringBuilder("");
            while ((line = in.readLine()) != null) {
                text.append(line.trim());
            }
            JSONObject result = JSONObject.parseObject(text.toString());
            System.out.println(result);
            if (result != null && result.getIntValue("status") == 0) {
                address = result.getJSONObject("result").getString("formatted_address");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return address;
    }

    /**
     * 将地址解析成经纬度
     *
     * @param address
     *            地址，例：浙江省杭州市西湖区
     * @return 返回经纬度数据。例：{"lng":120.08899292561351,"lat":30.207036169515438}
     */
    public static JSONObject getPosition(String address) {
        BufferedReader in = null;
        URL url = null;
        URLConnection connection = null;
        try {
            url = new URL(MAP_URL+"&address="+address);
            connection = url.openConnection();
            connection.setDoOutput(true);
            in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
            String line;
            StringBuilder text = new StringBuilder("");
            while ((line = in.readLine()) != null) {
                text.append(line.trim());
            }
            JSONObject result = JSONObject.parseObject(text.toString());
            if (result != null && result.getIntValue("status") == 0) {
                return result.getJSONObject("result").getJSONObject("location");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
//        System.out.println(getPosition("北京市东城区首都医科大学附属北京口腔医院"));
        System.out.println(getAddress(24.894969993261856,118.6195801523202));
    }







}
